﻿#请求CAMKE的最小构建版本
cmake_minimum_required(VERSION 3.5)
#设置项目名称
project(MyQt VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#在MSVC上必须指定 /Zc:__cplusplus 选项
if(MSVC)
	add_compile_options(/Zc:__cplusplus)
    #指定头文件包含目录
    #include_directories("D:/Qt/6.2.4/msvc2019_64/include")
endif()

#自动查找头文件路径函数(没有去重)
macro(FIND_INCLUDE_DIR result curdir)  #定义函数,2个参数:存放结果result；指定路径curdir；
    file(GLOB_RECURSE children "${curdir}/*.hpp" "${curdir}/*.h" )	#遍历获取{curdir}中*.hpp和*.h文件列表
    message(STATUS "children= ${children}")								#打印*.hpp和*.h的文件列表
    set(dirlist "")														#定义dirlist中间变量，并初始化
    foreach(child ${children})											#for循环
        string(REGEX REPLACE "(.*)/.*" "\\1" LIB_NAME ${child})			#字符串替换,用/前的字符替换/*h
        if(IS_DIRECTORY ${LIB_NAME})									#判断是否为路径
            LIST(APPEND dirlist ${LIB_NAME})							#将合法的路径加入dirlist变量中
        endif()															#结束判断
    endforeach()														#结束for循环
    set(${result} ${dirlist})											#dirlist结果放入result变量中
endmacro()																#函数结束

#查找include目录下的所有*.hpp,*.h头文件,并路径列表保存到 INCLUDE_DIR_LIST 变量中
FIND_INCLUDE_DIR(INCLUDE_SRC "Src" )	
FIND_INCLUDE_DIR(INCLUDE_TEST  "Test")	
include_directories(${INCLUDE_SRC})
include_directories(${INCLUDE_TEST})

#第三方库头文件加入
#include_directories(ThirdPpartyLibrary)
#自定义python类
FIND_INCLUDE_DIR(INCLUDE_MyPython  Python)	
include_directories(${INCLUDE_MyPython})
file(GLOB_RECURSE PythonFile "Python/*.cpp" "Src/Python/*.py")
#第Python3.1加入
set(PythonPath "D:/anaconda3")
set(PY_INCLUDE_DIR ${PythonPath}/include)
set(PY_LIB_DIR ${PythonPath}/libs)
include_directories(${PY_INCLUDE_DIR})
link_directories(${PY_LIB_DIR})

#递归搜索目录下的源文件添加到变量中
file(GLOB_RECURSE SrcFile  "Src/*.cpp" )
file(GLOB_RECURSE TestFile "Test/*.cpp")
file(GLOB_RECURSE ResFile  "resource/*.qrc")

# 指定引用的外部库的搜索路径
#LINK_DIRECTORIES(ThirdPpartyLibrary/curl)
#LINK_DIRECTORIES(ThirdPpartyLibrary/zlib)
#LINK_DIRECTORIES(ThirdPpartyLibrary/xlnt)


find_package(QT NAMES Qt6 REQUIRED COMPONENTS Widgets Core5Compat network Gui Sql Charts Core )
find_package(Qt6 REQUIRED COMPONENTS Widgets Core5Compat network Gui Sql Charts Core )


# 添加子目录 QXlsx
#add_subdirectory(QXlsx)

#设置可执行文件的输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/bin)
#添加参加编译的文件
add_executable(${PROJECT_NAME}
${TestFile}
${ResFile})

# 设置lib库输出目录
if (CMAKE_BUILD_TYPE MATCHES "Debug")
   set(LIBRARY_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/bin/Debug)
else()
   set(LIBRARY_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/bin/Release)
endif()

# 创建输出目录
file(MAKE_DIRECTORY ${LIBRARY_DIRECTORY})
# 所有不调用第三方库
add_library(xqt STATIC  ${SrcFile})
#target_include_directories(xqt PUBLIC ${INCLUDE_Src})
target_link_libraries(xqt PUBLIC Qt6::Widgets Qt6::Core5Compat Qt6::Network Qt6::Gui Qt6::Sql Qt6::Charts  Qt6::Core Qt6::GuiPrivate)#添加链接库
add_custom_command(TARGET xqt POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:xqt> ${LIBRARY_DIRECTORY})# 拷贝静态库文件

file(GLOB_RECURSE xqObjectCpp  "Src/Algorithm/*.cpp" "Src/XQAppInfo/*.cpp" "Src/XQEvent/*.cpp" "Src/XQModel/*.cpp" "Src/XSystem/*.cpp" )
add_library(xqObject STATIC  ${xqObjectCpp})
target_link_libraries(xqObject PUBLIC  Qt6::Widgets Qt6::Core5Compat Qt6::Network Qt6::Gui Qt6::Sql Qt6::Charts  Qt6::Core Qt6::GuiPrivate)#添加链接库
add_custom_command(TARGET xqObject POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:xqObject> ${LIBRARY_DIRECTORY})# 拷贝静态库文件

file(GLOB_RECURSE xqWidgetCpp  "Src/MyWidget/*.cpp" )
add_library(xqWidget STATIC  ${xqWidgetCpp})
target_link_libraries(xqWidget PUBLIC Qt6::Widgets Qt6::Core5Compat Qt6::Network Qt6::Gui Qt6::Sql Qt6::Charts Qt6::Core Qt6::GuiPrivate )#添加链接库
add_custom_command(TARGET xqWidget POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:xqWidget> ${LIBRARY_DIRECTORY})# 拷贝静态库文件

file(GLOB_RECURSE xqNetworkCpp  "Src/XQNetwork/*.cpp" )
add_library(xqNetwork STATIC  ${xqNetworkCpp})
target_link_libraries(xqNetwork PUBLIC Qt6::Widgets Qt6::Core5Compat Qt6::Network Qt6::Gui Qt6::Sql Qt6::Charts  Qt6::Core Qt6::GuiPrivate)#添加链接库
add_custom_command(TARGET xqNetwork POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:xqNetwork> ${LIBRARY_DIRECTORY})# 拷贝静态库文件

file(GLOB_RECURSE xqThreadCpp  "Src/XQThread/*.cpp" )
add_library(xqThread STATIC  ${xqThreadCpp})
target_link_libraries(xqThread PUBLIC Qt6::Widgets Qt6::Core5Compat Qt6::Network Qt6::Gui Qt6::Sql Qt6::Charts  Qt6::Core Qt6::GuiPrivate)#添加链接库
add_custom_command(TARGET xqThread POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:xqThread> ${LIBRARY_DIRECTORY})# 拷贝静态库文件

#install(TARGETS ${xqtdll} RUNTIME  DESTINATION ${CMAKE_CURRENT_LIST_DIR}/bin)#dll安装
#install(TARGETS ${xqtdll} ARCHIVE  DESTINATION ${CMAKE_CURRENT_LIST_DIR}/bin/lib)#lib安装

target_link_libraries(${PROJECT_NAME} 

xqt #xqObject xqWidget xqNetwork xqThread
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::GuiPrivate
Qt${QT_VERSION_MAJOR}::Widgets Qt6::Core5Compat Qt6::Network Qt6::Gui Qt6::Sql Qt6::Charts )